import { Component, Prop, Vue } from "vue-property-decorator";
@Component
export default class Original extends Vue {
  @Prop() private dayEat!: number;
  @Prop() private day!: number;
  
  // 食物表，原版
  foodData = [
    { foodName: "鸡腿肉(去皮)", ratio: 22, needWeight: 0 },
    { foodName: "牛肝(g)", ratio: 4, needWeight: 0 },
    { foodName: "牛心", ratio: 16, needWeight: 0 },
    { foodName: "鸡肝(g)", ratio: 4.3, needWeight: 0 },
    { foodName: "鸡心(g)", ratio: 15, needWeight: 0 },
    { foodName: "鸡蛋(g)", ratio: 9, needWeight: 0 },
    { foodName: "猪里脊(瘦肉/g)", ratio: 15.3, needWeight: 0 },
    { foodName: "太平洋马鲛鱼(鲅鱼)", ratio: 10.8, needWeight: 0 },
    { foodName: "新鲜淡菜(g)", ratio: 3.5, needWeight: 0 },
  ];
  // 营养成分
  nutritionData = [
    { name: "碳酸钙粉(g)", ratio: 0.6, needWeight: 0 },
    { name: "Nowfoods碘化钾(225mcg/粒)", ratio: 0.2, needWeight: 0 },
    { name: "维生素E软胶囊100mg/粒", ratio: 0.2, needWeight: 0 },
  ];
  countData() {
    const allEat = this.dayEat * this.day;
    this.foodData.map((item) => {
      item.needWeight = (allEat * item.ratio) / 100;
    });
    this.nutritionData.map((item) => {
      item.needWeight = (allEat * item.ratio) / 100;
    });
  }
  // 格式化表格
  formatRatio(row: any, column: any) {
    // 获取单元格数据
    const data = row[column.property];
    if (data == 0) {
      return 0;
    } else {
      return data + "%";
    }
  }
  // 格式化食材
  formatWeight(row: any, column: any) {
    const num = parseFloat(row[column.property]).toFixed(3);
    const s = num.substring(0, num.length - 1) + "g"; //只取小数位2位
    return (
      s &&
      s.toString().replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
        return $1 + ",";
      })
    );
  }
  mounted() {
    this.countData();
  }
}
